import java.util.HashMap;
import java.util.Map;

//两数之和
//测试链接 https://leetcode.cn/problems/two-sum/
public class TwoSum {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> hash = new HashMap<>(); // <nums[i], i>
        for (int i = 0; i < nums.length; i++) {
            int x = target - nums[i];
            if (hash.containsKey(x)) {
                return new int[] { i, hash.get(x) };
            }
            hash.put(nums[i], i);
        }
        // 照顾编译器
        return new int[] { -1, -1 };
    }
}
